package cn.zjutleo.village_server.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.*;

/**
 * @Author: 张坤
 * @DateTime: 2021/9/7 22:48
 * @Description: 该类用于 给前端返回json地图数据,或其他
 */
@Service
public class JsonService {
    public String getBusInfo(String busInfo){
        return readJsonFile("busInfo"+busInfo);
    }
    /**
    * @Author: 张坤
    * @Description: 读取areaInfo文件下的文件
     * @return
    */
    public String getAreaInfo(String areaCode){
        JSONArray jsonArray=JSONArray.parseArray(readJsonFile("areaInfo/all"));

        if(areaCode.length()==2){
            return readJsonFile("areaInfo/data/"+areaCode+"/"+areaCode);
        }
        else if (areaCode.length()==4){
            return readJsonFile("areaInfo/data/"+areaCode.substring(0,2)+"/"+areaCode+"/"+areaCode);
        }
        else if (areaCode.length()==6){
            return readJsonFile("areaInfo/data/"+areaCode.substring(0,2)+"/"+areaCode.substring(0,4)+"/"+areaCode+"/"+areaCode);
        }
        else if (areaCode.length()==9){
            return readJsonFile("areaInfo/data/"+areaCode.substring(0,2)+"/"+areaCode.substring(0,4)+"/"+areaCode.substring(0,6)+"/"+areaCode+"/"+areaCode);
        }
        return null;
    }
    /**
     *
     * @param mapName 要读取的省市的Json地图文件内容
     * @return String json文件内容
     */
    public String getMapJson(String mapName){
        return readJsonFile("map/"+mapName);
    }
    /**
     *
     * @param filePath 要读取的Json文件名称
     * @return String json文件内容
     */
    public  String readJsonFile(String filePath){
        File jsonFile = null;
        try {
            System.out.println("读取文件"+filePath);
            //读取文件内容
            jsonFile = ResourceUtils.getFile("classpath:json/"+filePath+".json");
            //将json转化为String
            String json = FileUtils.readFileToString(jsonFile);
            return json;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "找不到文件";
    }
    public static String readJsonAbsoluteFilePath(String filePath){
        File jsonFile = null;
        try {
            System.out.println("读取文件"+filePath);
            //读取文件内容
            jsonFile = ResourceUtils.getFile(filePath);
            //将json转化为String
            String json = FileUtils.readFileToString(jsonFile);
            return json;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "找不到文件";
    }
    public static void writeJsonFile(String path, String str){
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
            osw.write(str);
            osw.flush();//清空缓冲区，强制输出数据
            osw.close();//关闭输出流
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
    * @Author: 张坤
    * @Description: 省市县镇村 数据生成算法 需保留areaInfo下的五个文件
     * 分别为: areas.json,cities.json,provinces.json,streets.json,villages.json
    */
    public static void operation2(){
        String basePath="D:\\springCode\\digital_village\\src\\main\\resources\\json\\areaInfo\\";
        // 省/直辖市
        String provincesStr=readJsonAbsoluteFilePath(basePath+"provinces.json");
        JSONArray provincesJSONArray = JSONArray.parseArray(provincesStr);
        // 市
        String citiesStr=readJsonAbsoluteFilePath(basePath+"cities.json");
        JSONArray citiesJSONArray = JSONArray.parseArray(citiesStr);
        // 县/区
        String areasStr=readJsonAbsoluteFilePath(basePath+"areas.json");
        JSONArray areasJSONArray = JSONArray.parseArray(areasStr);
        // 街道/镇
        String streetsStr=readJsonAbsoluteFilePath(basePath+"streets.json");
        JSONArray streetsJSONArray = JSONArray.parseArray(streetsStr);
        // 村
        String villagesStr=readJsonAbsoluteFilePath(basePath+"villages.json");
        JSONArray villagesJSONArray = JSONArray.parseArray(villagesStr);
        basePath=basePath+"data\\";

        for(int i=0;i<provincesJSONArray.size();i++){
            String code= (String) provincesJSONArray.getJSONObject(i).get("code");
            createDirect(basePath+code);

            JSONObject parentJSONObject=provincesJSONArray.getJSONObject(i);
            JSONArray writeInfoJSON = new JSONArray();
            for(int j=0;j<citiesJSONArray.size();j++){
                JSONObject sunJSONObject=citiesJSONArray.getJSONObject(j);
                if(parentJSONObject.get("code").equals(sunJSONObject.get("provinceCode"))){
                    JSONObject temp=new JSONObject();
                    temp.put("name",sunJSONObject.get("name"));
                    temp.put("code",sunJSONObject.get("code"));
                    writeInfoJSON.add(temp);
                }
            }
            writeJsonFile(basePath+code+"\\"+code+".json",writeInfoJSON.toJSONString());
        }
        for(int i=0;i<citiesJSONArray.size();i++){
            String code= (String) citiesJSONArray.getJSONObject(i).get("code");
            createDirect(basePath+code.substring(0,2)+"\\"+code);

            JSONObject parentJSONObject=citiesJSONArray.getJSONObject(i);
            JSONArray writeInfoJSON = new JSONArray();
            for(int j=0;j<areasJSONArray.size();j++){
                JSONObject sunJSONObject=areasJSONArray.getJSONObject(j);
                if(parentJSONObject.get("code").equals(sunJSONObject.get("cityCode"))){
                    JSONObject temp=new JSONObject();
                    temp.put("name",sunJSONObject.get("name"));
                    temp.put("code",sunJSONObject.get("code"));
                    writeInfoJSON.add(temp);
                }
            }
            writeJsonFile(basePath+code.substring(0,2)+"\\"+code+"\\"+code+".json",writeInfoJSON.toJSONString());
        }
        for(int i=0;i<areasJSONArray.size();i++){
            String code= (String) areasJSONArray.getJSONObject(i).get("code");
            createDirect(basePath+code.substring(0,2)+"\\"+code.substring(0,4)+"\\"+code);

            JSONObject parentJSONObject=areasJSONArray.getJSONObject(i);
            JSONArray writeInfoJSON = new JSONArray();
            for(int j=0;j<streetsJSONArray.size();j++){
                JSONObject sunJSONObject=streetsJSONArray.getJSONObject(j);
                if(parentJSONObject.get("code").equals(sunJSONObject.get("areaCode"))){
                    JSONObject temp=new JSONObject();
                    temp.put("name",sunJSONObject.get("name"));
                    temp.put("code",sunJSONObject.get("code"));
                    writeInfoJSON.add(temp);
                }
            }
            writeJsonFile(basePath+code.substring(0,2)+"\\"+code.substring(0,4)+"\\"+code+"\\"+code+".json",writeInfoJSON.toJSONString());
        }
        for(int i=0;i<streetsJSONArray.size();i++){
            String code= (String) streetsJSONArray.getJSONObject(i).get("code");
            createDirect(basePath+code.substring(0,2)+"\\"+code.substring(0,4)+"\\"+code.substring(0,6)+"\\"+code);

            JSONObject parentJSONObject=streetsJSONArray.getJSONObject(i);
            JSONArray writeInfoJSON = new JSONArray();
            for(int j=0;j<villagesJSONArray.size();j++){
                JSONObject sunJSONObject=villagesJSONArray.getJSONObject(j);
                if(parentJSONObject.get("code").equals(sunJSONObject.get("streetCode"))){
                    JSONObject temp=new JSONObject();
                    temp.put("name",sunJSONObject.get("name"));
                    temp.put("code",sunJSONObject.get("code"));
                    writeInfoJSON.add(temp);
                }
            }
            writeJsonFile(basePath+code.substring(0,2)+"\\"+code.substring(0,4)+"\\"+code.substring(0,6)+"\\"+code+"\\"+code+".json",writeInfoJSON.toJSONString());
        }
    }
    public static void createDirect(String path){
        System.out.println(path);
        File file=new File(path);
        if(!file.exists()){//如果文件夹不存在
            file.mkdir();//创建文件夹
        }
    }
    public static void main(String[] args) {
        operation2();
    }
}
